# -*- coding: utf-8 -*-
"""
-------------------------------------------
    Description:
    Author:             yu_lei
    Email:              lyu@topxgun.com
    Date:               2022/03/31
-------------------------------------------
"""
import json
import sys

from PyQt5 import QtWidgets
# from data.data_parser import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

# 顶部菜单，遥测面板
from data.DataHandler import datahandler


# from views.ui_telf import *


class TelDataPad(QWidget):
    title_font = QFont("Roman times", 15, QFont.Bold)

    def __init__(self):
        super().__init__()
        # self.parser = DataHandler()
        # self.parser.receiveDataA.connect(self.sig_telA)
        # self.parser.receiveDataB.connect(self.sig_telB)
        # self.parser.receiveDataC.connect(self.sig_telC)
        # self.parser.receiveDataD.connect(self.sig_telD)
        # self.parser.receiveDataE.connect(self.sig_telE)
        #
        # self.tel_f_pad = TelFPad()
        self.initui()

    def initui(self):
        datahandler.record_on = False  # 关闭数据记录功能
        datahandler.upload_switch(0)  # 关闭上行数据发送功能

        self.setWindowTitle('遥测数据')
        self.setGeometry(0, 0, 1400, 800)

        main_box = QVBoxLayout()
        main_box.setSpacing(10)
        self.setLayout(main_box)

        main_tab_widget = QTabWidget(self)
        main_box.addWidget(main_tab_widget)
        style_enable = 'font: 12pt "Noto Sans S Chinese DemiLight"; color: rgb(0, 0, 0);'
        main_tab_widget.setStyleSheet(style_enable)

        self.readout_RecvServoData = QLabel("")
        self.readout_RecvFccSystmData = QLabel("")
        self.readout_RecvSensorINS = QLabel("")
        self.readout_RecvSensorBINS = QLabel("")
        self.readout_RecvSensorADC = QLabel("")
        self.readout_RecvSensorADC2 = QLabel("")
        self.readout_RecvSensorGPS = QLabel("")
        self.readout_RecvSensorRadar = QLabel("")
        self.readout_RecvDataLink = QLabel("")
        self.readout_RecvElecSystem = QLabel("")
        self.readout_RecvEngineSystem = QLabel("")
        self.readout_RecvWarningSystem = QLabel("")
        self.readout_RecvEquipment = QLabel("")
        self.readout_RecvCommandEcho = QLabel("")

        self.add_display_tab(main_tab_widget, self.readout_RecvServoData, "舵系统")
        self.add_display_tab(main_tab_widget, self.readout_RecvFccSystmData, "飞控系统")

        self.add_display_tab(main_tab_widget, self.readout_RecvSensorINS, "主惯导")
        self.add_display_tab(main_tab_widget, self.readout_RecvSensorBINS, "备用惯导")
        self.add_display_tab(main_tab_widget, self.readout_RecvSensorADC, "大气机")
        self.add_display_tab(main_tab_widget, self.readout_RecvSensorADC2, "备用大气机")
        self.add_display_tab(main_tab_widget, self.readout_RecvSensorGPS, "GPS")
        self.add_display_tab(main_tab_widget, self.readout_RecvSensorRadar, "雷达")
        self.add_display_tab(main_tab_widget, self.readout_RecvDataLink, "数据链")
        self.add_display_tab(main_tab_widget, self.readout_RecvElecSystem, "配电盒")
        self.add_display_tab(main_tab_widget, self.readout_RecvEngineSystem, "发动机")
        self.add_display_tab(main_tab_widget, self.readout_RecvWarningSystem, "告警信息")
        self.add_display_tab(main_tab_widget, self.readout_RecvEquipment, "机载设备")
        self.add_display_tab(main_tab_widget, self.readout_RecvCommandEcho, "指令反馈")

        datahandler.sig_RecvServoData.connect(
            lambda data: self.readout_RecvServoData.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvFccSystmData.connect(
            lambda data: self.readout_RecvFccSystmData.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvSensorINS.connect(
            lambda data: self.readout_RecvSensorINS.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvSensorBINS.connect(
            lambda data: self.readout_RecvSensorBINS.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvSensorADC.connect(
            lambda data: self.readout_RecvSensorADC.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvSensorADC2.connect(
            lambda data: self.readout_RecvSensorADC2.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvSensorGPS.connect(
            lambda data: self.readout_RecvSensorGPS.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvSensorRadar.connect(
            lambda data: self.readout_RecvSensorRadar.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvDataLink.connect(lambda data: self.readout_RecvDataLink.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvElecSystem.connect(
            lambda data: self.readout_RecvElecSystem.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvEngineSystem.connect(
            lambda data: self.readout_RecvEngineSystem.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvWarningSystem.connect(
            lambda data: self.readout_RecvWarningSystem.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvEquipment.connect(
            lambda data: self.readout_RecvEquipment.setText(json.dumps(data, indent=4)))
        datahandler.sig_RecvCommandEcho.connect(
            lambda data: self.readout_RecvCommandEcho.setText(json.dumps(data, indent=4)))

    def add_display_tab(self, main_tab_widget, readout_label: QWidget, tab_name: str):
        grid_space = 10
        tab_servo_system = QWidget(self)
        grid = QGridLayout(tab_servo_system)
        grid.setSpacing(grid_space)
        main_tab_widget.addTab(tab_servo_system, tab_name)

        scrollArea = QtWidgets.QScrollArea()
        scrollArea.setWidgetResizable(True)
        scrollArea.setObjectName("scrollArea")
        # scrollAreaWidgetContents = QtWidgets.QWidget()
        # scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 780, 535))
        # scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        scrollArea.setWidget(readout_label)
        # verticalLayout.addWidget(self.scrollArea)

        grid.addWidget(scrollArea, 0, 0)


# 地面站软件的入口程序
if __name__ == '__main__':
    # 启动地面站主界面
    app = QtWidgets.QApplication(sys.argv)
    gcs_main_window = QtWidgets.QMainWindow()
    ui = TelDataPad()

    ui.show()

    sys.exit(app.exec_())
